package com.pirestupppai.widgets.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pirestupppai.R;
import com.pirestupppai.bean.TupaiUser;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态推荐视图
 *
 * @author: xiaoluo
 * @date: 2017-04-13 15:21
 */
public class FeedRecommendLayout extends RelativeLayout {
    private Context mContext;

    private RelativeLayout mParent;
    private LinearLayout mFeedRecommendLayout;
    private List<TupaiUser> mUserList = new ArrayList<>();

    public FeedRecommendLayout(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public FeedRecommendLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public FeedRecommendLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.widget_feed_recommend, this, true);
        mFeedRecommendLayout = (LinearLayout) this.findViewById(R.id.feed_recommend_layout);
        mParent = (RelativeLayout) this.findViewById(R.id.recommend_parent);
    }

    public void setRecommendData() {
        mParent.setVisibility(VISIBLE);
        mFeedRecommendLayout.removeAllViews();
        mUserList.clear();
        for (int i = 0; i < 10; i++) {
            TupaiUser user = new TupaiUser();
            user.setNickname("用户" + i);
            mUserList.add(user);
        }

        for (int i = 0; i < mUserList.size(); i++) {
            SingleRecommendView view = new SingleRecommendView(mContext, new SingleRecommendView.OnCloseRecommendListener() {
                @Override
                public void onClose() {
                    boolean isAllGone = true;
                    for (int i = 0; i < mFeedRecommendLayout.getChildCount(); i++) {
                        if (mFeedRecommendLayout.getChildAt(i).getVisibility() != GONE) {
                            isAllGone = false;
                            break;
                        }
                    }
                    if (isAllGone) {
                        mParent.setVisibility(GONE);
                    }
                }
            });
            view.setData(mUserList.get(i));
            mFeedRecommendLayout.addView(view);
        }
    }
}
